
#include "src/gui/ChatDialog.h"

#include <qlayout.h>
//Added by qt3to4:
#include <QVBoxLayout>
#include <QHBoxLayout>

#define DEFAULT_BACKGROUND Qt::black
#define DEFAULT_FOREGROUND Qt::white
#define LOGFILE "logs/sessionLog.txt"

ChatDialog::ChatDialog() {
  // This gives the window minimize and maximize buttons.
  this->setWindowFlags( Qt::Window );

  this->setCaption("GiTMchat Battle.net Chat Client");

  // Set the background to black.
  QPalette palette;
  palette.setColor( QPalette::Base, QColor(DEFAULT_BACKGROUND) );
  palette.setColor( QPalette::Text, QColor(DEFAULT_FOREGROUND) );
  palette.setColor( QPalette::Foreground, QColor(DEFAULT_FOREGROUND) );
  palette.setColor( QPalette::WindowText, QColor(DEFAULT_FOREGROUND) );
  palette.setColor( QPalette::AlternateBase, QColor(DEFAULT_BACKGROUND) );
  palette.setColor( QPalette::Background, QColor(DEFAULT_BACKGROUND) );
  palette.setColor( QPalette::Window, QColor(DEFAULT_BACKGROUND) );

  setSizeGripEnabled(true);

  file_.setName( LOGFILE );
  file_.open( QIODevice::WriteOnly | QIODevice::Append );
  
  logFile_.setDevice(&file_);
  

  chatInput = new QLineEdit(this);
  chatInput->setPalette( palette );
  sendButton = new QPushButton(tr("Send"), this);
  chatWindow = new QTextEdit(this);
  chatWindow->setPalette( palette );
  chatWindow->setMinimumSize(640, 480);
  chatWindow->setReadOnly(true);


  QHBoxLayout *bottom = new QHBoxLayout;
  bottom->addWidget(chatInput);
  bottom->addWidget(sendButton);
  bottom->addSpacing(20);
  QVBoxLayout *mainLayout = new QVBoxLayout(this);
  mainLayout->addWidget(chatWindow);
  mainLayout->addLayout(bottom);

  connect(sendButton, SIGNAL(clicked()),
	  this, SLOT(sendButtonClicked()));
}

ChatDialog::~ChatDialog() {
  file_.close();
}


void ChatDialog::sendButtonClicked() 
{
    emit sendText( chatInput->text() );
    chatInput->clear();
}

void ChatDialog::appendChat( QString msg, QString type ) 
{
    QString endlStr( "\n" );
#ifdef Q_OS_WIN32
    endlStr = QString( "\r\n" );
#endif

    if( type == "system" ) 
    {
        logFile_ << msg << endlStr;
        chatWindow->setColor( QColor( Qt::red ) );
    }
    else if( type == "chat" ) 
    {
        logFile_ << msg << endlStr;
        chatWindow->setColor( QColor( Qt::green ) );
    }
    else if( type == "whisper" )
    {
        logFile_ << msg << endlStr;
        chatWindow->setColor( QColor( Qt::darkGreen ) );
    }
    else if( type == "channel" ) 
    {
        logFile_ << msg << endlStr;
        chatWindow->setColor( QColor( Qt::magenta ) );
    }
    else if( type == "server" ) 
    {
        logFile_ << msg << endlStr;
        chatWindow->setColor( QColor( Qt::yellow ) );
    }
    else if( type == "me" ) 
    {
        logFile_ << msg << endlStr;
        chatWindow->setColor( QColor( Qt::cyan ) );
    }
    else if( type == "debug" ) 
    {
        chatWindow->setColor( QColor( Qt::gray ) );
    }
    else 
    {
        logFile_ << msg << endlStr;
    }

    chatWindow->append( msg );
    chatWindow->setColor( QColor( DEFAULT_FOREGROUND ) );
  
}
